import * as Cesium from 'cesium';

class SpaceGrid {
  constructor(viewer, arr = [110, 40, 0]) {
    this.viewer = viewer;
    this.cubeSize = 0;
    this.SpaceGridPrimitive = null
    this.modelMatrix = Cesium.Transforms.eastNorthUpToFixedFrame(Cesium.Cartesian3.fromDegrees(arr[0], arr[1], arr[2]));
  }
  // 生成矩阵顶点
  generateVertex (n) {
    const arr = [
      new Cesium.Cartesian3(0.0, 0.0, 0.0),                     // 0
      new Cesium.Cartesian3(this.cubeSize, 0.0, 0.0),           // 1
      new Cesium.Cartesian3(this.cubeSize, this.cubeSize, 0.0), // 2
      new Cesium.Cartesian3(0.0, this.cubeSize, 0.0),           // 3
    ];
    let newArr = [];
    for (let i = 0; i < arr.length; i++) {
      let start = arr[i];
      let end = arr[(i + 1) % arr.length];
      let stepX = (end.x - start.x) / (n + 1);
      let stepY = (end.y - start.y) / (n + 1);
      let stepZ = (end.z - start.z) / (n + 1);
      for (let j = 0; j <= n; j++) {
        let newPoint = new Cesium.Cartesian3(
          start.x + j * stepX,
          start.y + j * stepY,
          start.z + j * stepZ
        );
        newArr.push(newPoint);
      }
    }
    return newArr;
  }
  generateIndexes (n) {
    const indices = [];
    for (let i = 0; i < 4; i++) {
      const start = i * (n + 2);
      const end = (i + 1) % 4 * (n + 2);
      indices.push([start, end]);
    }
    for (let index = 0; index < n + 1; index++) {
      indices.push([index + 1, (4 + n * 4) - n + 1 - index])
      indices.push([n + 3 + index, (4 + (n + 1) * 4) - index - 1])
    }
    return indices;
  }
  drawGrid (regionSize, level) {
    if (this.SpaceGridPrimitive) {
      this.viewer.scene.primitives.remove(this.SpaceGridPrimitive)
      this.SpaceGridPrimitive = null
    }
    this.cubeSize = regionSize
    // 线的外观
    const linepma = new Cesium.PerInstanceColorAppearance({
      flat: true,
      translucent: false,
      renderState: {
        depthTest: { enabled: true },
        lineWidth: Math.min(5.0, this.viewer.scene.maximumAliasedLineWidth),
      },
      material: Cesium.Material.fromType('Color', { color: Cesium.Color.GRAY })
    })
    const vertices = this.generateVertex(level)
    const edges = this.generateIndexes(level - 1);
    let pos64Arr = [];
    edges.forEach((edge) => {
      let start = vertices[edge[0]];
      let end = vertices[edge[1]];
      pos64Arr.push(start.x, start.y, start.z, end.x, end.y, end.z);
    });
    const geometryAttribute = new Cesium.GeometryAttribute({
      componentDatatype: Cesium.ComponentDatatype.DOUBLE,
      componentsPerAttribute: 3,
      values: new Float64Array(pos64Arr)
    });
    let mygeometry = new Cesium.Geometry({
      attributes: {
        position: geometryAttribute
      },
      primitiveType: Cesium.PrimitiveType.LINES,
      boundingSphere: Cesium.BoundingSphere.fromVertices(pos64Arr)
    });
    const geometryInstances = new Cesium.GeometryInstance({
      geometry: mygeometry,
      attributes: {
        color: Cesium.ColorGeometryInstanceAttribute.fromColor(Cesium.Color.fromCssColorString('#F7F7F7'))
      }
    })
    this.SpaceGridPrimitive = new Cesium.Primitive({
      geometryInstances: geometryInstances,
      appearance: linepma,
      modelMatrix: this.modelMatrix,
      asynchronous: false
    });
    this.viewer.scene.primitives.add(this.SpaceGridPrimitive);
  }
}

export default SpaceGrid;